﻿// $Id$
/*! \file
 * \brief Definition of Northwind.DomainModel.Order.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml.Serialization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataRules;
#if (!NET_CF)
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
#endif

namespace Northwind.DomainModel
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary />
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial interface IOrderContainer
    {
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary />
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial interface IOrderMapper:
        IDataMapper
    {
        /// <summary />
        DomainEntityKeyedCollection<Order> FindByCustomer(Customer customer, DataMapperCollection dataMappers);
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary />
    ///
    ///////////////////////////////////////////////////////////////////////////

#if (!NET_CF)
    [Serializable]
    [DataObject]
#endif
    public partial class Order:
        DomainEntity<Order.PrimaryKey, Order.DataTransfer>
    {

        /// <summary />
#if (!NET_CF)
        [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
#endif
        public partial class PrimaryKey:
            PrimaryKey<int>
        {

            /// <summary />
            public PrimaryKey():
                base()
            {
            }

            /// <summary />
            public PrimaryKey(int key):
                base(key)
            {
            }

        }

        /// <summary />
        [SoapType("OrderData", "http://www.codeplex.com/salamanca/northwind/domainmodel/")]
        [XmlType("OrderData", Namespace="http://www.codeplex.com/salamanca/northwind/domainmodel/")]
#if (!NET_CF)
        [Serializable]
        [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
#endif
        public partial class DataTransfer:
            IDataTransferObject
        {
        }

        /// <summary />
        public Order():
            base()
        {
        }

        /// <summary />
        public Order(DataMapperCollection dataMappers):
            base(dataMappers)
        {
        }

#if (!NET_CF)
        /// <summary />
        protected Order(SerializationInfo info, StreamingContext context):
            base(info, context)
        {
        }
#endif

        /// <summary />
        protected override void OnDeleting(PersistenceEventArgs e)
        {
            base.OnDeleting(e);

            // Composition
            _LoadOrderDetails(true);
            _OrderDetails.Delete(e.Context);
        }

        /// <summary />
        protected override void OnSaving(PersistenceEventArgs e)
        {
            if (_Customer!=null)
              ((IPersistent)_Customer).Save(e.Context);
            if (_Employee!=null)
              ((IPersistent)_Employee).Save(e.Context);
            if (_Shipper!=null)
              ((IPersistent)_Shipper).Save(e.Context);
            if (_OrderDetails!=null)
              _OrderDetails.Save(e.Context);

            base.OnSaving(e);
        }

        /// <summary />
        public override void Load(bool lazyLoad)
        {
            base.Load(lazyLoad);

            if (!IsNew)
            {
                _LoadCustomer(lazyLoad);
                _LoadEmployee(lazyLoad);
                _LoadShipper(lazyLoad);
            }
        }

        /// <summary />
        public override void Unload()
        {
            base.Unload();

            _Customer=null;
            _Employee=null;
            _Shipper=null;
        }

        /// <summary />
        public static IList<Order> FindByCustomer(Customer customer, DataMapperCollection dataMappers)
        {
            return ((IOrderMapper)dataMappers[typeof(Order)]).FindByCustomer(customer, dataMappers);
        }

        /// <summary />
        private void _LoadOrderDetails(bool lazyLoad)
        {
            if (_OrderDetails==null)
            {
                if (IsNew)
                    _OrderDetails=new DomainEntityKeyedCollection<OrderDetails>();
                else
                    _OrderDetails=(DomainEntityKeyedCollection<OrderDetails>)DomainModel.OrderDetails.FindByOrder(this, DataMappers);

                _OrderDetails.Saving+=delegate(object s, DomainEntityCollectionSavingEventArgs<OrderDetails> e) {
                    foreach (OrderDetails o in e.AddedItems)
                        o.Order=this;
                    foreach (OrderDetails o in e.RemovedItems)
                        if (o.Order==this)
                            o.Delete(e.Context);
                };
            }
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public Customer Customer
        {
            get
            {
                return _CustomerProperty;
            }
            set
            {
                if (_CustomerProperty!=value)
                {
                    _CustomerProperty=value;
                    RaisePropertyChanged("Customer");
                }
            }
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public Employee Employee
        {
            get
            {
                return _EmployeeProperty;
            }
            set
            {
                if (_EmployeeProperty!=value)
                {
                    _EmployeeProperty=value;
                    RaisePropertyChanged("Employee");
                }
            }
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public Shipper Shipper
        {
            get
            {
                return _ShipperProperty;
            }
            set
            {
                if (_ShipperProperty!=value)
                {
                    _ShipperProperty=value;
                    RaisePropertyChanged("Shipper");
                }
            }
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public System.DateTime? OrderDate
        {
            get
            {
                return _OrderDateProperty;
            }
            set
            {
                if (_OrderDateProperty!=value)
                {
                    _OrderDateProperty=value;
                    RaisePropertyChanged("OrderDate");
                }
            }
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public System.DateTime? RequiredDate
        {
            get
            {
                return _RequiredDateProperty;
            }
            set
            {
                if (_RequiredDateProperty!=value)
                {
                    _RequiredDateProperty=value;
                    RaisePropertyChanged("RequiredDate");
                }
            }
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public System.DateTime? ShippedDate
        {
            get
            {
                return _ShippedDateProperty;
            }
            set
            {
                if (_ShippedDateProperty!=value)
                {
                    _ShippedDateProperty=value;
                    RaisePropertyChanged("ShippedDate");
                }
            }
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public decimal? Freight
        {
            get
            {
                return _FreightProperty;
            }
            set
            {
                if (_FreightProperty!=value)
                {
                    _FreightProperty=value;
                    RaisePropertyChanged("Freight");
                }
            }
        }

        /// <summary />
        [StringLengthRule(40)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string ShipName
        {
            get
            {
                return _ShipNameProperty;
            }
            set
            {
                if (_ShipNameProperty!=value)
                {
                    _ShipNameProperty=value;
                    RaisePropertyChanged("ShipName");
                }
            }
        }

        /// <summary />
        [StringLengthRule(60)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string ShipAddress
        {
            get
            {
                return _ShipAddressProperty;
            }
            set
            {
                if (_ShipAddressProperty!=value)
                {
                    _ShipAddressProperty=value;
                    RaisePropertyChanged("ShipAddress");
                }
            }
        }

        /// <summary />
        [StringLengthRule(15)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string ShipCity
        {
            get
            {
                return _ShipCityProperty;
            }
            set
            {
                if (_ShipCityProperty!=value)
                {
                    _ShipCityProperty=value;
                    RaisePropertyChanged("ShipCity");
                }
            }
        }

        /// <summary />
        [StringLengthRule(15)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string ShipRegion
        {
            get
            {
                return _ShipRegionProperty;
            }
            set
            {
                if (_ShipRegionProperty!=value)
                {
                    _ShipRegionProperty=value;
                    RaisePropertyChanged("ShipRegion");
                }
            }
        }

        /// <summary />
        [StringLengthRule(10)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string ShipPostalCode
        {
            get
            {
                return _ShipPostalCodeProperty;
            }
            set
            {
                if (_ShipPostalCodeProperty!=value)
                {
                    _ShipPostalCodeProperty=value;
                    RaisePropertyChanged("ShipPostalCode");
                }
            }
        }

        /// <summary />
        [StringLengthRule(15)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string ShipCountry
        {
            get
            {
                return _ShipCountryProperty;
            }
            set
            {
                if (_ShipCountryProperty!=value)
                {
                    _ShipCountryProperty=value;
                    RaisePropertyChanged("ShipCountry");
                }
            }
        }

        /// <summary />
        public Collection<OrderDetails> OrderDetails
        {
            get
            {
                _LoadOrderDetails(true);
                return new Collection<OrderDetails>(_OrderDetails);
            }
        }

        private DomainEntityKeyedCollection<OrderDetails> _OrderDetails;

    }
}
